package com.zjm.find.service.impl;

import com.zjm.find.dao.SuggestionMapper;
import com.zjm.find.dao.UserFollowMapper;
import com.zjm.find.dao.UserHistoryMapper;
import com.zjm.find.dao.UserMapper;
import com.zjm.find.enmu.WXParams;
import com.zjm.find.pojo.*;
import com.zjm.find.service.UserService;
import com.zjm.find.utils.HttpClientUtil;
import com.zjm.find.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @Author 涛涛
 * @Create 2021/1/17
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SuggestionMapper suggestionMapper;

    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private UserHistoryMapper userHistoryMapper;

    @Override
    public WXSessionModel wxAuthorization(String code) {
        Map<String, String> param = new HashMap<>();
        param.put("appid", WXParams.APPID);
        param.put("secret", WXParams.SECRET);
        param.put("js_code", code);
        param.put("grant_type", WXParams.ANTHORIZATION_CODE);
        String wxResult = HttpClientUtil.doGet(WXParams.WX_ANTHORIZATION_URL, param);
        WXSessionModel model = JsonUtils.jsonToPojo(wxResult, WXSessionModel.class);
        return model;
    }

    @Override
    public User checkUserExistByOpenId(User user) {
        return userMapper.checkUserExistByOpenId(user);
    }

    @Override
    public void updateUserInfoByOpenId(User user) {
        userMapper.updateUserInfoByOpenId(user);
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public List allUser(User user) {
        return userMapper.allUser(user);
    }

    @Override
    public boolean createSuggestion(Suggestion suggestion) {
        return suggestionMapper.insert(suggestion) == 1 ? true : false;
    }

    @Override
    public boolean createFollow(UserFollow userFollow) {
        return userFollowMapper.createFollow(userFollow) == 1 ? true : false;
    }

    @Override
    public boolean cancelFollow(UserFollow userFollow) {
        return userFollowMapper.cancelFollow(userFollow) == 1 ? true : false;
    }

    @Override
    public List<UserFollow> selectFollow(UserFollow userFollow) {
        return userFollowMapper.selectFollow(userFollow);
    }

    @Override
    public List selectUserFollow(UserFollow userFollow) {
        return userFollowMapper.selectUserFollow(userFollow);
    }

    @Override
    public boolean createHistory(UserHistory userHistory) {
        if (!userHistoryMapper.selectHistory(userHistory).isEmpty()) {
            return userHistoryMapper.updateHistoryTime(userHistory) == 1 ? true : false;
        } else {
            return userHistoryMapper.createHistory(userHistory) == 1 ? true : false;

        }
    }

    @Override
    public boolean deleteHistory(UserHistory userHistory) {
        return userHistoryMapper.deleteHistory(userHistory) == 1 ? true : false;
    }

    @Override
    public List selectHistory(UserHistory userHistory) {
        return userHistoryMapper.selectHistory(userHistory);
    }

}
